package com.castle.fortress.admin.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsChannelAuditDto;
import com.castle.fortress.admin.cms.entity.CmsChannelAuditEntity;
import com.castle.fortress.admin.cms.mapper.CmsChannelAuditMapper;
import com.castle.fortress.admin.cms.service.CmsChannelAuditService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * cms栏目审核配置表 服务实现类
 *
 * @author castle
 * @since 2022-03-23
 */
@Service
public class CmsChannelAuditServiceImpl extends ServiceImpl<CmsChannelAuditMapper, CmsChannelAuditEntity> implements CmsChannelAuditService {
    /**
    * 获取查询条件
    * @param cmsChannelAuditDto
    * @return
    */
    public QueryWrapper<CmsChannelAuditEntity> getWrapper(CmsChannelAuditDto cmsChannelAuditDto){
        QueryWrapper<CmsChannelAuditEntity> wrapper= new QueryWrapper();
        if(cmsChannelAuditDto != null){
            CmsChannelAuditEntity cmsChannelAuditEntity = ConvertUtil.transformObj(cmsChannelAuditDto,CmsChannelAuditEntity.class);
            wrapper.like(cmsChannelAuditEntity.getId() != null,"id",cmsChannelAuditEntity.getId());
            wrapper.like(cmsChannelAuditEntity.getChannelId() != null,"channel_id",cmsChannelAuditEntity.getChannelId());
            wrapper.like(cmsChannelAuditEntity.getAuditId() != null,"audit_id",cmsChannelAuditEntity.getAuditId());
        }
        return wrapper;
    }


    @Override
    public IPage<CmsChannelAuditDto> pageCmsChannelAudit(Page<CmsChannelAuditDto> page, CmsChannelAuditDto cmsChannelAuditDto) {
        QueryWrapper<CmsChannelAuditEntity> wrapper = getWrapper(cmsChannelAuditDto);
        Page<CmsChannelAuditEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsChannelAuditEntity> cmsChannelAuditPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsChannelAuditDto> pageDto = new Page(cmsChannelAuditPage.getCurrent(), cmsChannelAuditPage.getSize(),cmsChannelAuditPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsChannelAuditPage.getRecords(),CmsChannelAuditDto.class));
        return pageDto;
    }


    @Override
    public List<CmsChannelAuditDto> listCmsChannelAudit(CmsChannelAuditDto cmsChannelAuditDto){
        QueryWrapper<CmsChannelAuditEntity> wrapper = getWrapper(cmsChannelAuditDto);
        List<CmsChannelAuditEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsChannelAuditDto.class);
    }

    @Override
    public void removeByChannelId(Long channelId) {
        QueryWrapper<CmsChannelAuditEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("channel_id",channelId);
        baseMapper.delete(wrapper);
    }

    @Override
    public void removeByChannelIds(List<Long> channelIds) {
        QueryWrapper<CmsChannelAuditEntity> wrapper=new QueryWrapper<>();
        wrapper.in("channel_id",channelIds);
        baseMapper.delete(wrapper);
    }

    @Override
    public List<Map<String, Object>> getAuditorByChannelId(Long channelId) {
        List<Map<String, Object>> list = baseMapper.getAuditorByChannelId(channelId);
        if(list!=null && !list.isEmpty()){
            for(Map<String, Object> m:list){
                if(channelId.toString().equals(m.get("channel_id").toString())){
                    m.put("isSelf",true);
                }else{
                    m.put("isSelf",false);
                }
                m.put("id",m.get("id").toString());
            }
        }
        return list;
    }

    @Override
    public void removeByParentId(Long id) {
        baseMapper.removeByParentId(id);
    }
}

